package com.lovo.project.service.impl;

import com.lovo.project.dao.PermissionDAO;
import com.lovo.project.enums.PermissionCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PermissionDO;
import com.lovo.project.service.PermissionService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/4 14:35
 */
public class PermissionServiceImpl implements PermissionService {
    @Override
    public void addPermission(PermissionDO permissionDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDAO dao = sqlSession.getMapper(PermissionDAO.class);
        try {
            int i = dao.insertPermission(permissionDO);
            if (i<=0) {
                throw new OurException(PermissionCodeEnum.PERMISSION_ADD_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public void updatePermission(PermissionDO permissionDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDAO dao = sqlSession.getMapper(PermissionDAO.class);
        try {
            int i = dao.updatePermission(permissionDO);
            if (i<=0) {
                throw new OurException(PermissionCodeEnum.PERMISSION_UPDATE_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }


    }

    @Override
    public PermissionDO findPermissionById(Integer permissionId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDAO dao = sqlSession.getMapper(PermissionDAO.class);
        try {
            PermissionDO permissionDO = dao.selectPermissionById(permissionId);
            if (permissionDO == null) {
                throw new OurException(PermissionCodeEnum.PERMISSION_FIND_ERROR);
            }
            return permissionDO;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public List<PermissionDO> listPermissionByInfo(String permissionName) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDAO dao = sqlSession.getMapper(PermissionDAO.class);

        try {
            List<PermissionDO> list = dao.selectPermissionByInfo(permissionName);
            if (list == null) {
                throw new OurException(PermissionCodeEnum.PERMISSION_FIND_ERROR);
            }
            return list;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }


    }

    @Override
    public void deletePermissionById(Integer permissionId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        PermissionDAO dao = sqlSession.getMapper(PermissionDAO.class);
        try {
            int i = dao.deletePermissionById(permissionId);
            if (i<=0) {
                throw new OurException(PermissionCodeEnum.PERMISSION_DELETE_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }

    }
}
